//
//  RankViewController.m
//  edu
//
//  Created by baobaodp on 13-8-27.
//  Copyright (c) 2013年 baobaodp. All rights reserved.
//

#import "RankViewController.h"
#import "RankListViewController.h"
#import "ChartViewController.h"
#import "EduViewController.h"
#import "DBbase.h"
#import "Record.h"
#import "LineNodeChart.h"
#import "PieChart.h"

#import "TopScrollView.h"
#import "RootScrollView.h"

@interface RankViewController ()

@end

@implementation RankViewController

@synthesize condition;
@synthesize score;
@synthesize classRank;
@synthesize gradeRank;
@synthesize stuName;
@synthesize tttview;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self loadData];
    
//    //--------------------------------------------------------
//    UIImageView *setButtonShadowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(258, 0, 22, 44)];
//    setButtonShadowImageView.image = [UIImage imageNamed:@"top_channelbutton_shadow.png"];
//    setButtonShadowImageView.userInteractionEnabled = YES;
//    [self.view addSubview:setButtonShadowImageView];
//    
//    UIImageView *topShadowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 44, 320, 5)];
//    [topShadowImageView setImage:[UIImage imageNamed:@"top_background_shadow.png"]];
//    [self.view addSubview:topShadowImageView];
//    
//    TopScrollView *topView =[[TopScrollView alloc] initWithDelegate:self];
//    RootScrollView *rootView = [[RootScrollView alloc] initWithTopTab:topView];
//    topView.rootView =rootView;
//    
//    [self.tttview addSubview:topView];
//    [self.tttview addSubview:rootView];
//    
////    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 320, 50)];
////    label.text = @"333333";
////    label.textAlignment = NSTextAlignmentCenter;
////    label.font = [UIFont boldSystemFontOfSize:50.0];
////    [self.tttview addSubview:label];
//
//    //--------------------------------------------------------
}

-(void) loadData
{
    self.stuName.text = [userEntry.userInfo getFieldValueByName:@"stu_Name"];
    NSString *ptn_Id = [self.condition getFieldValueByName:@"ptn_Id"];
    
    NSString *stu_Id = [userEntry.userInfo getFieldValueByName:@"stu_Id"];
    
    NSString *class_Id = [userEntry.userInfo getFieldValueByName:@"class_Id"];
    NSString *class_Ym = [userEntry.userInfo getFieldValueByName:@"class_Ym"];
    NSString *class_Div = [userEntry.userInfo getFieldValueByName:@"class_Div"];
    
    NSString *querySql = [NSString stringWithFormat:@"select * from T_Rank where ptn_Id ='%@' and stu_Id = '%@' and (class_Id = '%@' or (class_Id is null and class_Ym = '%@' and class_Div = '%@')) and rank_Type='2'  " ,ptn_Id,stu_Id,class_Id,class_Ym,class_Div];
    
    const char *sql = [querySql UTF8String];
    
    NSMutableArray * ranks = getDbRecords(sql,@"T_Rank",[NSArray arrayWithObjects:@"Rank_Id", nil]);
    
    for(Record *recordStt in ranks){
        if([[recordStt getFieldValueByName:@"rank_Div"] isEqual:@"1"]){
            self.score.text = [recordStt getFieldValueByName:@"R_Souce"];
            self.classRank.text = [recordStt getFieldValueByName:@"rank"];
        }else if([[recordStt getFieldValueByName:@"rank_Div"] isEqual:@"2"]){
            self.gradeRank.text = [recordStt getFieldValueByName:@"rank"];
        }
    }

    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)classClick:(id)sender
{
    RankListViewController *detailViewController = [[RankListViewController alloc] initWithNibName:@"RankListViewController" bundle:nil];
    
	[self.navigationController pushViewController:detailViewController animated:YES];
    
    detailViewController.condition = self.condition;
    detailViewController.rank_Div = @"1";
}
- (IBAction)gradeClick:(id)sender
{
    RankListViewController *detailViewController = [[RankListViewController alloc] initWithNibName:@"RankListViewController" bundle:nil];
    
	[self.navigationController pushViewController:detailViewController animated:YES];
    
    detailViewController.condition = self.condition;
    detailViewController.rank_Div = @"2";

}

- (IBAction)chartClick:(id)sender
{
    [self openChar:@"班级排名曲线图" div:@"1" type:@"rank" seriesType:@"area" valueReverse :TRUE];
}

- (IBAction)chartClick2:(id)sender
{
    [self openChar:@"班级成绩曲线图" div:@"1" type:@"R_Souce" seriesType:@"area" valueReverse :FALSE];
}

- (IBAction)chartClick3:(id)sender
{
    [self openChar:@"年级排名曲线图" div:@"2" type:@"rank" seriesType:@"line"valueReverse :TRUE];
}

- (IBAction)chartClick4:(id)sender
{
    [self openChar:@"年级成绩曲线图" div:@"2" type:@"R_Souce" seriesType:@"line" valueReverse :FALSE];
}

- (IBAction)chartClick5:(id)sender
{
    ChartViewController *detailViewController = [[ChartViewController alloc] initWithNibName:@"ChartViewController" bundle:nil];
    
    PieChart *chart = [[PieChart alloc]init];
    
    //DATA
    NSString *ptn_Id = [self.condition getFieldValueByName:@"ptn_Id"];
    
    NSString *stu_Id = [userEntry.userInfo getFieldValueByName:@"stu_Id"];
    
    NSString *querySql = [NSString stringWithFormat:@"select distinct T1.* ,T4.* "
                          "from T_Result T1 left join T_LessonTch T2 on T1.lesson_Tch_Id = T2.lesson_Tch_Id left join T_Lesson T3 on T2.lesson_ID = T3.lesson_ID left join M_Lesson T4 on T3.lesson_CD = T4.lesson_CD where T1.ptn_Id ='%@' and T1.stu_Id = '%@' " ,ptn_Id,stu_Id];
 
    const char *sql = [querySql UTF8String];
    
    
    chart.title = @"成绩分布";
    
    [chart addSeries:@"R_Souce" name:@"lesson_Name"];
    
    chart.dataSource = getDbRecords(sql,@"T_Rank",[NSArray arrayWithObjects:@"Rank_Id", nil]);
    
    
    
    detailViewController.chart = chart;
    
    
    
	[self.navigationController pushViewController:detailViewController animated:YES];
}

-(void) openChar:(NSString *) title div:(NSString *) rank_Div  type:(NSString *)type  seriesType:(NSString *)seriesType valueReverse :(BOOL)valueReverse
{

    ChartViewController *detailViewController = [[ChartViewController alloc] initWithNibName:@"ChartViewController" bundle:nil];
    
    LineNodeChart *chart = [[LineNodeChart alloc]init];
    
    //DATA
    NSString *lessonSql = [NSString stringWithFormat:@"select distinct T1.lesson_Tch_Id ,T4.* "
                           "from T_Rank T1 left join T_LessonTch T2 on T1.lesson_Tch_Id = T2.lesson_Tch_Id left join T_Lesson T3 on T2.lesson_ID = T3.lesson_ID left join M_Lesson T4 on T3.lesson_CD = T4.lesson_CD where  T1.stu_Id = '%@' " ,[userEntry.userInfo getFieldValueByName:@"stu_Id"]];
    
    const char *lessql = [lessonSql UTF8String];
    
    NSMutableArray * lessons = getDbRecords(lessql,@"T_Rank",[NSArray arrayWithObjects:@"Rank_Id", nil]);
    
    
    NSString *querySql = [self getNodesDataSqlWithType:type lessons:lessons rank_Div:rank_Div];
    
    const char *sql = [querySql UTF8String];
    
    chart.title = title;
    chart.seriesType = seriesType;
    chart.valueReverse = valueReverse;
    chart.dataSource = getDbRecords(sql,@"T_Rank",[NSArray arrayWithObjects:@"Rank_Id", nil]);
    
    chart.categoryAxisField = @"TP_Name";
    
    for (Record * les in lessons) {
        if([[les getFieldDataByName:@"lesson_Tch_Id"] isEqual:@""]){
            [chart addSeries:@"总分" name:@"总分"];
        }else{
            [chart addSeries:[les getFieldValueByName:@"lesson_Name"] name:[les getFieldValueByName:@"lesson_Name"]];
        }
        
    }
    
    detailViewController.chart = chart;
    
    
    
	[self.navigationController pushViewController:detailViewController animated:YES];
}

- (NSString *) getNodesDataSqlWithType :(NSString *) type lessons:(NSMutableArray *) lessons rank_Div:(NSString *)rank_Div
{
    NSMutableString *tmp = [NSMutableString stringWithCapacity:8];
    
    [tmp appendString:@"select "];
    
    for (Record * les in lessons) {
        
        if([[les getFieldValueByName:@"lesson_Tch_Id"] isEqual:@""]){
            [tmp appendFormat:@" sum( case when T1.lesson_Tch_Id is null then T1.%@ else 0 end) as '%@',"
             ,type, @"总分"];
        }else{
            [tmp appendFormat:@" sum( case when T1.lesson_Tch_Id ='%@' then T1.%@ else 0 end) as '%@',"
             , [les getFieldValueByName:@"lesson_Tch_Id"],type
             , [les getFieldValueByName:@"lesson_Name"]];
        }
        
        
    }
    [tmp appendFormat:@" T1.ptn_Id ,T2.TP_Name  from T_Rank T1 left join T_TestPtn T2 on T1.ptn_Id = T2.ptn_Id where  T1.stu_Id = '1' and T1.rank_Div = '%@' group by T1.ptn_Id",rank_Div];
    
    
    return tmp;
}

/**
 * 获取tab的全部显示名称
 */
-(NSArray *) slideSwitchTabNames :(TopScrollView *)topView
{
    return [NSArray arrayWithObjects:@"kk", @"gg", nil];;
}

/**
 *获取相应index的view
 */
-(UIView *) slideSwitch :(TopScrollView *)topView viewOnIndex:(NSInteger) index
{
    return nil;
}

@end
